package cn.fl.ref.bscrateinfo.controller;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cmm.mid.core.framework.page.DataResultVO;
import cmm.mid.core.framework.page.ListPageVO;
import cmm.mid.core.framework.page.Pager;
import cmm.web.core.framework.controller.BaseController;
import cn.cmm.base.framework.constant.BaseConstant;
import cn.cmm.base.framework.constant.BaseExceptionConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.DateUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.ref.bscrateinfo.service.IBscRateInfoService;
import cn.fl.ref.bscrateinfo.transform.BscRateInfoVOTransform;
import cn.fl.ref.bscrateinfo.vo.BscRateInfoVO;

import com.alibaba.fastjson.JSONObject;

import fl.ref.bscrateinfo.condition.BscRateInfoQueryCondition;
import fl.ref.bscrateinfo.dto.BscRateInfoDTO;

/**
 * @Description: 基础利率信息
 */
@Controller
@RequestMapping("/rfbase/bscrateinfo")
public class BscRateInfoController extends BaseController {

	private static final Logger logger = LoggerFactory.getLogger(BscRateInfoController.class);

	@Autowired
	private IBscRateInfoService bscRateInfoService;

	/**
	 * 到列表页面
	 */
	@RequestMapping(value = "/toListPage.do", method = { RequestMethod.GET })
	public ModelAndView toListPage(HttpServletRequest request, HttpServletResponse response) {
		logger.info("加载基础利率信息列表页面.");
		ModelAndView view = new ModelAndView("/rfbase/bscrateinfo/bscRateInfoList");
		return view;
	}
	
	/**
	 * 到新增修改页面
	 */
	@RequestMapping(value = "/toAddPage.do", method = { RequestMethod.GET, RequestMethod.POST })
	public ModelAndView toAddPage(HttpServletRequest request, HttpServletResponse response) {
		ModelAndView view = new ModelAndView("/rfbase/bscrateinfo/bscRateInfoAdd");
		String id = request.getParameter("id");
		view.addObject("id", id);
		return view;
	}

	/**
	 * 初始化列表
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/initList.do", method = { RequestMethod.GET })
	@ResponseBody
	public Object initList(HttpServletRequest request, HttpServletResponse response) {
		String currentPage = request.getParameter("page");
		String pageSize = request.getParameter("pagesize");

		Pager page = new Pager();
		if (StringUtils.isNotEmpty(currentPage)) {
			page.setCurrentPage(Integer.parseInt(currentPage));
		}

		if (StringUtils.isNotEmpty(pageSize)) {
			page.setEveryPage(Integer.parseInt(pageSize));
		}
		BscRateInfoQueryCondition qc = new BscRateInfoQueryCondition();

		page.setCondition(qc);

		Pager pager = this.bscRateInfoService.searchListPage(page);
		List<BscRateInfoDTO> list = (List<BscRateInfoDTO>) pager.getResultList();
		List<BscRateInfoVO> voList = BscRateInfoVOTransform.toVOList(list);
		ListPageVO<BscRateInfoVO> listPage = new ListPageVO<BscRateInfoVO>(voList, pager.getTotalCount());

		BaseLogUtils.info(logger, "initList", "查询数据完成. DATA:" + JSONObject.toJSONString(page), CurrentThreadContext.getCurrentUserName());
		return listPage;
	}


	/**
	 * 新增
	 */
	@RequestMapping(value = "/add.do", method = { RequestMethod.GET, RequestMethod.POST })
	@ResponseBody
	public Object add(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String mdfTm = request.getParameter("mdfTm");
		String halfY = request.getParameter("halfY");
		String oneY = request.getParameter("oneY");
		String threeY = request.getParameter("threeY");
		String fiveY = request.getParameter("fiveY");
		String moreY = request.getParameter("moreY");
		String rem = request.getParameter("rem");

		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, "新增成功!");
		try {
			// 验证
			if (StringUtils.isEmpty(mdfTm)) {
				throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, "修改时间不能为空!");
			}
			BscRateInfoVO entity = new BscRateInfoVO();
			if (StringUtils.isNotEmpty(halfY)) {
				entity.setHalfY(new BigDecimal(halfY));
			}
			if (StringUtils.isNotEmpty(oneY)) {
				entity.setOneY(new BigDecimal(oneY));
			}
			if (StringUtils.isNotEmpty(threeY)) {
				entity.setThreeY(new BigDecimal(threeY));
			}
			if (StringUtils.isNotEmpty(fiveY)) {
				entity.setFiveY(new BigDecimal(fiveY));
			}
			if (StringUtils.isNotEmpty(moreY)) {
				entity.setMoreY(new BigDecimal(moreY));
			}
			entity.setMdfTm(DateUtils.formatStringToDate(mdfTm, DateUtils.YYYY_MM_DD));
			entity.setMdfUsrId(CurrentThreadContext.getCurrentUserId()); // 修改人
			entity.setRem(rem);
			entity.setCrtUsrId(CurrentThreadContext.getCurrentUserId()); // 新增人
			entity.setCrtTm(new Date()); // 新增时间
			entity.setIsValid(BaseConstant.IS_YESNO_YES); // 有效
			entity.setIsDel(BaseConstant.IS_YESNO_NO); // 未删除

			Long mainId = this.bscRateInfoService.add(entity);
			result.setSuccess(BaseConstant.IS_YESNO_YES);
			result.setInfo("新增成功!");
			BaseLogUtils.info(logger, "add", "新增数据 ", CurrentThreadContext.getCurrentUserName());
		} catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.info(logger, "add", "新增数据失败" + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		} catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("新增失败!");
			BaseLogUtils.error(logger, "add", "新增数据失败" + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}
		return result;
	}

	/**
	 * 修改
	 * 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/modify.do", method = { RequestMethod.GET, RequestMethod.POST })
	@ResponseBody
	public Object modify(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String id = request.getParameter("id");
		String mdfTm = request.getParameter("mdfTm");
		String halfY = request.getParameter("halfY");
		String oneY = request.getParameter("oneY");
		String threeY = request.getParameter("threeY");
		String fiveY = request.getParameter("fiveY");
		String moreY = request.getParameter("moreY");
		String rem = request.getParameter("rem");

		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, "修改成功!");
		try {
			// 验证
			if (StringUtils.isEmpty(mdfTm)) {
				throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, "修改时间不能为空!");
			}
			BscRateInfoVO entity = new BscRateInfoVO();
			entity.setId(Long.valueOf(id));
			if (StringUtils.isNotEmpty(halfY)) {
				entity.setHalfY(new BigDecimal(halfY));
			}
			if (StringUtils.isNotEmpty(oneY)) {
				entity.setOneY(new BigDecimal(oneY));
			}
			if (StringUtils.isNotEmpty(threeY)) {
				entity.setThreeY(new BigDecimal(threeY));
			}
			if (StringUtils.isNotEmpty(fiveY)) {
				entity.setFiveY(new BigDecimal(fiveY));
			}
			if (StringUtils.isNotEmpty(moreY)) {
				entity.setMoreY(new BigDecimal(moreY));
			}
			entity.setMdfTm(DateUtils.formatStringToDate(mdfTm, DateUtils.YYYY_MM_DD));
			entity.setMdfUsrId(CurrentThreadContext.getCurrentUserId()); // 修改人
			entity.setRem(rem);
			this.bscRateInfoService.modify(entity);

			result.setSuccess(BaseConstant.IS_YESNO_YES);
			result.setInfo("更新成功!");
			BaseLogUtils.info(logger, "modify", "修改数据,id[" + id + "].", CurrentThreadContext.getCurrentUserName());
		} catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.info(logger, "modify", "修改数据失败,id[" + id + "]." + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		} catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("系统错误");
			BaseLogUtils.error(logger, "modify", "修改数据失败,id[" + id + "]." + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}
		return result;
	}

	/**
	 * 删除 逻辑删除
	 * 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/remove.do", method = { RequestMethod.GET, RequestMethod.POST })
	@ResponseBody
	public Object remove(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String id = request.getParameter("id");

		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, "修改成功!");
		try {
			BscRateInfoVO entity = new BscRateInfoVO();
			entity.setId(Long.valueOf(id));
			entity.setIsDel(BaseConstant.IS_YESNO_YES);
			entity.setMdfUsrId(CurrentThreadContext.getCurrentUserId()); // 修改人
			this.bscRateInfoService.modify(entity);
			result.setSuccess(BaseConstant.IS_YESNO_YES);
			result.setInfo("删除成功!");
			BaseLogUtils.info(logger, "modify", "修改数据,id[" + id + "].", CurrentThreadContext.getCurrentUserName());
		} catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.info(logger, "modify", "修改数据失败,id[" + id + "]." + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		} catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("系统错误");
			BaseLogUtils.error(logger, "modify", "修改数据失败,id[" + id + "]." + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}
		return result;
	}
	
	/**
	 * 得到一条记录
	 */
	@RequestMapping(value = "/getById.do", method = { RequestMethod.GET, RequestMethod.POST })
	@ResponseBody
	public Object getById(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String id = request.getParameter("id");
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, "获取成功!");
		BscRateInfoVO bscRateInfoVO = null;
		try {
			bscRateInfoVO = this.bscRateInfoService.selectById(Long.valueOf(id));
			result.setData(bscRateInfoVO);
		} catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.info(logger, "getById", "获取数据失败,主键[" + id + "]." + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		} catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("系统错误!");
			BaseLogUtils.error(logger, "getById", "获取数据失败,主键[" + id + "]." + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}
		return result;
	}

}
